package com.zhangxi.springbootwebzhangxi.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhangxi.springbootwebzhangxi.pojo.Emp;
import com.zhangxi.springbootwebzhangxi.pojo.PageBean;
import com.zhangxi.springbootwebzhangxi.pojo.Result;
import com.zhangxi.springbootwebzhangxi.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * ClassName: EmpController
 * Package: com.zhangxi.springbootwebzhangxi.controller
 */
@RestController
@Slf4j
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

//    @GetMapping
//    public Result page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
//        //记录日志
//        log.info("分页查询，参数：{},{}", page, pageSize);
//        //调用业务层分页查询功能
//        PageBean pageBean = empService.page(page, pageSize);
//        return Result.success(pageBean);
//    }

//    增强版本
@GetMapping
public Result page(@RequestParam(defaultValue = "1") Integer page,
                   @RequestParam(defaultValue = "10") Integer pageSize,
                   String name, Short gender,
                   @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                   @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        //记录日志

        log.info("分页查询，参数：{},{},{},{},{},{}", page, pageSize,name, gender, begin, end);
        PageBean pageBean = empService.list(page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);

    }

//    @GetMapping("/page")
//    public Result pageTwo(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
//        PageBean pb = new PageBean();
//        //记录日志
//        log.info("分页查询，参数：{},{}", page, pageSize);
//        // 开启分页查询 PageHelper插件
//        PageHelper.startPage(page, pageSize);
//        List<Emp> empList = empService.list();
//        Page<Emp> p = (Page<Emp>) empList;
//        //把数据填充到PageBean中
//        pb.setTotal(p.getTotal());
//        pb.setRows(p.getResult());
//        return Result.success(pb);
//    }


    @DeleteMapping("/{ids}")
    public Result deleteEmp(@PathVariable List<Integer> ids) {
        //记录日志
        log.info("删除员工, ids:{}",ids);
        empService.deleteEmp(ids);
        return Result.success();
    }

    @PostMapping
    public Result Insert(@RequestBody Emp emp){
        //记录日志
        log.info("新增员工, emp:{}",emp);
       empService.insertEmp(emp);
       return Result.success();
    }
    //根据id查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }
    //修改员工
    @PutMapping
    public Result update(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success();
    }

}
